package de.openkanban.client.ui.presenter;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;

import de.openkanban.client.util.RootPanel;
import de.openkanban.shared.dto.BoardDto;

/**
 * Callback that handles get default board request
 */
public class GetDefaultBoardByUserIdRequestCallback implements RequestCallback {

	@Override
	public void onError(Request request, Throwable exception) {

	}

	@Override
	public void onResponseReceived(Request request, Response response) {
		BoardDto boardDto = new BoardDto(response.getText());
		runWhiteBoardInitialization(boardDto);
	}

	private void runWhiteBoardInitialization(BoardDto boardDto) {
		if (isNewBoard(boardDto)) {
			RootPanel.activateBoardEditMode(true);
		} else {
			RootPanel.main.getBoard().getWhiteBoardView().loadActivities(boardDto);
			RootPanel.activateBoardEditMode(false);
		}
	}

	private boolean isNewBoard(BoardDto boardDto) {
		return boardDto.getBoardId() == null;
	}

}
